<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\facade\Builder;
use app\common\model\Level;
use think\facade\View;
//该文件为自动生成，请按需修改
class Coupon extends Base
{
    //所有定义变量均为演示，请根据实际需求更改

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Coupon';
    protected string $productModel = '\app\common\model\Product';

    //关联模型
    protected array $hasOne = [];
    protected array $append = ['types_name'];

    //附加字段或者虚拟字段

    public function index(): string
    {
        //构建表格数据
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'title' => '全选', 'fixed' => 'left'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '标题', 'modify' => '2,input'])
            ->addColumn(['field' => 'money', 'title' => '金额', 'modify' => '2,input'])
            ->addColumn(['field' => 'sale_money', 'title' => '优惠金额'])
            ->addColumn(['field' => 'num', 'title' => '优惠券数量'])
            ->addColumn(['field' => 'end_times', 'title' => '活动有效期'])
            ->addColumn(['field' => 'types', 'title' => '优惠券类型', 'templet' => '<div>{{d.types_name}}</div>'])
            ->addColumn(['field' => 'time', 'title' => '有效期天数', 'modify' => '2,number','templet' => '#sort_time', 'width' => 120])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'modify' => '2,input', 'templet' => '#sort_sort', 'width' => 120])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 260])
            ->addRightButtons(['edit' => ['size' => '1000,1000'], 'delete', 
                'receive' => [
                    'size' => '1300,1000',
                    'title' => '领取优惠券记录',
                    'url'   => url('usercoupon/index')->build(),
                    'event' => '_openNewPage'
                ]
            ])
            ->addTopButtons(['add' => ['size' => '1000,1000'], 'deleteAll'])
            ->addSearch([
                ['title', '标题搜索', 'input', '', '', '', 'like']
            ])
            ->addSwitchTool([['name' => 'status', 'pk' => $this->pk, 'text' => '上架|下架']])
            ->addSortTool([['name' => 'sort', 'pk' => $this->pk, 'url' => url('update')->build()],['name' => 'time', 'pk' => $this->pk, 'url' => url('update')->build()]])
            ->fetch();
    }
    public static function getTypes(): array
    {
        return [1 => '普通优惠券', 2 => '订单满减优惠券'];
    }
    public function add($tpl = 'coupon/edit')
    {   $list=$this->productModel::where(['is_del'=>0])->order('sort asc')->select();
        $Level =Level::where(['is_del'=>0])->order('id asc')->select();
        View::assign('list', $list);
        View::assign('chekid', []);
        View::assign('levelid', []);
        View::assign('Level', $Level);
        return View::fetch($tpl);
    }

    public function edit($id = '', $tpl = 'coupon/edit', $info = '')
    {
        $info = $this->model::find($id);
        $list=$this->productModel::where(['is_del'=>0])->order('sort asc')->select();
        $Level =Level::where(['is_del'=>0])->order('id asc')->select();
        $chekid=explode(',',$info['product_ids']);
        $levelid= !empty($info['level_ids'])?explode(',',$info['level_ids']):[];
        View::assign('levelid', $levelid);
        View::assign('chekid', $chekid);
        View::assign('list', $list);
        View::assign('Level', $Level);
        return parent::edit($id, $tpl, $info);
    }
    public function savepsot()
    {
        return json($this->model::savepsot($this->request->post())); // TODO: Change the autogenerated stub
    }
}
